package com.b2.service.impl;

import com.b2.constant.MessageConstant;
import com.b2.context.BaseContext;
import com.b2.mapper.ClassMapper;
import com.b2.mapper.ParentClassMapper;
import com.b2.mapper.ParentMapper;
import com.b2.pojo.dto.addParentDto;
import com.b2.pojo.entity.Class;
import com.b2.pojo.entity.Parent;
import com.b2.pojo.entity.ParentClass;
import com.b2.service.ParentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 悦神
* @description 针对表【parent】的数据库操作Service实现
* @createDate 2024-04-26 10:47:56
*/
@Service
public class ParentServiceImpl extends ServiceImpl<ParentMapper, Parent>
    implements ParentService{

    @Autowired
    ClassMapper classMapper;
    @Autowired
    ParentMapper parentMapper;
    @Autowired
    ParentClassMapper parentClassMapper;
    @Override
    public void addparent(addParentDto parent) {
        String code = parent.getCode();
        LambdaQueryWrapper<Class> o = new LambdaQueryWrapper<>();
        o.eq(Class::getCode2, code);
        Class aClass = classMapper.selectOne(o);

        if (aClass==null){
            throw new RuntimeException(MessageConstant.CODE_FAILED);
        }
        if (aClass.getTeacherId()==null){
            throw new RuntimeException(MessageConstant.VOID_TEACHER);
        }
        Parent parent1 = new Parent();
        parent1.setId(BaseContext.getCurrentId());
        parent1.setPhone(parent.getPhone());
        parent1.setName(parent.getName());
        parent1.setKindergartenName(parent.getKindergartenName());
        parentMapper.insert(parent1);
        ParentClass parentClass = new ParentClass();
        parentClass.setParentId(parent1.getId());
        parentClass.setClassId(aClass.getId());
        parentClassMapper.insert(parentClass);
    }
}




